package servlet;

import pojo.ClientUsers;
import service.ClientUsersDaoManager;
import service.factory.ManagerFactory;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class RegistServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static ClientUsersDaoManager clientUsersDaoManager;

	public RegistServlet() {
		super();
		clientUsersDaoManager = ManagerFactory.getClientUserDaoManager();
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String account = request.getParameter("account");
		String psd = request.getParameter("psd");
		String uname = request.getParameter("username");
		System.out.println(account+"   "+psd+"    "+uname);
		ClientUsers user = new ClientUsers(null, uname, psd, null);
		int addResult = clientUsersDaoManager.add(user);
		if(addResult == 1)
		{
			request.getSession().setAttribute("user", user);
			response.sendRedirect("login.html");
		}
		else{
			request.getSession().setAttribute("errorInfo", "Your username or password are userd!");
			request.getSession().setAttribute("user", user);
			response.sendRedirect("regist.html");
		}
	}

}
